import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Header from '../../component/Pan/Header';
import Main from '../../component/Pan/Main';
import { hideMenuList } from '../../redux/actions/menuListVisible';
import { isLogin } from '../../api';
import { COMMON_SUCCESS_STATE, LOGIN_PATH, STORE_KEY } from '../../constant';
import { setStore } from '../../utils/storageUtils';
import { setUser } from '../../redux/actions/user';
import { hideDataMenu } from '../../redux/actions/dataMenu';
import { hideTransfer } from '../../redux/actions/transferVisible';
import { hideFileDataMenu } from '../../redux/actions/fileDataMenu';

class Pan extends PureComponent {

  static propTypes = {
    hideMenuList: PropTypes.func,
    history: PropTypes.object,
    setUser: PropTypes.func,
    hideDataMenu: PropTypes.func,
    hideTransfer: PropTypes.func,
    hideFileDataMenu: PropTypes.func
  };

  // 判断用户是否登录，未登录转入登录页面，登录存入redux中
  isLogin = async () => {
    const { setUser } = this.props
    const {
      code,
      data
    } = await isLogin();
    if (code === COMMON_SUCCESS_STATE) {
      // 存入store中
      setStore(STORE_KEY, data);
      // 设置进redux
      setUser(data)
    } else {
      this.props.history.replace(LOGIN_PATH);
    }
  };

  //  取消出现的工具栏
  cancelTool = (event) => {
    const { hideDataMenu, hideTransfer, hideMenuList, hideFileDataMenu } = this.props
    event.stopPropagation()
    hideDataMenu()
    hideTransfer()
    hideMenuList()
    hideFileDataMenu()
  }

  componentDidMount () {
    this.isLogin();
  }

  render () {
    return (
      <div onClick={this.cancelTool}>
        <Header/>
        <Main/>
      </div>
    );
  }
}

export default connect(
  // eslint-disable-next-line no-unused-vars
  (state) => ({
  }),
  {
    hideMenuList,
    setUser,
    hideDataMenu,
    hideTransfer,
    hideFileDataMenu
  },
)(Pan);
